import 'package:get/get.dart';
import 'package:o2oa_all_platform/common/utils/index.dart';

import '../../../../common/api/x_file_assemble_control.dart';
import '../../../../common/models/clouddisk/zone_info.dart';
import '../../../../common/models/enums/o2_contact_pick_mode.dart';
import '../../../../common/models/orgs/o2_contact_picker.dart';
import '../../../../common/models/sheet_menu_data.dart';
import '../../../../common/widgets/o2_stateless_widget.dart';
import '../../../home/contact/contact_picker/index.dart';
import 'index.dart';

class EnterpriseZonePermissionController extends GetxController {
  EnterpriseZonePermissionController();

  final state = EnterpriseZonePermissionState();

  CloudDiskZoneInfo? _currentZone;

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    final map = Get.arguments;
    if (map == null || map is! CloudDiskZoneInfo) {
      OLogger.e('参数不正确！');
      Get.back();
      return;
    }
    _currentZone = map;
    getZoneInfo();
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }

  getZoneInfo() async {
    final result =
        await FileAssembleService.to.getZone(_currentZone?.zoneId ?? '');
    if (result != null) {
      state.title = result.name ?? '';
      state.zonePermissionList.clear();
      state.zonePermissionList.addAll(result.zonePermissionList ?? []);
    }
  }

  roleName(String role) {
    switch (role) {
      case 'admin':
        return 'cloud_disk_v3_zone_role_admin'.tr;
      case 'editor':
        return 'cloud_disk_v3_zone_role_editor'.tr;
      case 'reader':
        return 'cloud_disk_v3_zone_role_reader'.tr;
      default:
        return 'cloud_disk_v3_zone_role_reader'.tr;
    }
  }

  clickZonePermission(CloudDiskZoneInfoPermission item) {
    OLogger.d('点击了 权限 $item');
    final context = Get.context;
    if (context == null) {
      return;
    }
    List<SheetMenuData> menus = [];
    menus.add(SheetMenuData(
        'cloud_disk_v3_zone_role_reader'.tr, () => _setRole('reader', item)));
    menus.add(SheetMenuData(
        'cloud_disk_v3_zone_role_editor'.tr, () => _setRole('editor', item)));
    menus.add(SheetMenuData(
        'cloud_disk_v3_zone_role_admin'.tr, () => _setRole('admin', item)));
    menus.add(SheetMenuData(
        'cloud_disk_buttons_delete'.tr, () => _removePermission(item)));
    O2UI.showBottomSheetWithMenuData(context, menus);
  }

  _setRole(String role, CloudDiskZoneInfoPermission item) async {
    item.role = role;
    _savePermission2Server([item]);
  }

  _removePermission(CloudDiskZoneInfoPermission item) {
    final context = Get.context;
    if (context == null) {
      return;
    }
    O2UI.showConfirm(context, 'cloud_disk_v3_zone_permission_delete_confirm'.tr,
        okPressed: () => {_removePermissionOnline(item)});
  }

  _removePermissionOnline(CloudDiskZoneInfoPermission item) async {
    final result = await FileAssembleService.to
        .removeZonePermission(_currentZone?.zoneId ?? '', [item]);
    if (result != null) {
      getZoneInfo();
    }
  }

  clickAddPermission() async {
    OLogger.d('点击了 添加权限');
    final result = await ContactPickerPage.startPicker([
      ContactPickMode.personPicker,
      ContactPickMode.departmentPicker,
    ], multiple: true);
    if (result != null && result is ContactPickerResult) {
      final users =
          (result.users ?? []).map((e) => e.distinguishedName ?? '').toList();
      final units = (result.departments ?? [])
          .map((e) => e.distinguishedName ?? '')
          .toList();
      if (users.isEmpty && units.isEmpty) {
        return;
      }
      List<CloudDiskZoneInfoPermission> permissions = [];
      for (var item in users) {
        permissions
            .add(CloudDiskZoneInfoPermission(name: item, role: 'reader'));
      }
      for (var item in units) {
        permissions
            .add(CloudDiskZoneInfoPermission(name: item, role: 'reader'));
      }
      _savePermission2Server(permissions);
    }
  }

  _savePermission2Server(List<CloudDiskZoneInfoPermission> permissions) async {
    final result = await FileAssembleService.to
        .saveZonePermission(_currentZone?.zoneId ?? '', permissions);
    if (result != null) {
      getZoneInfo();
    }
  }
}
